﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VehicleScale.Models;

namespace VehicleScale.EntityMappers
{
    public class CategoryMapper
    {
        #region Methods With Relation
        public static CategoryModel ToModel(Category entity)
        {
            if (entity == null) return null;
            CategoryModel model = new CategoryModel()
            {
                CategoryID = entity.CategoryID,
                CategoryName = entity.CategoryName,
                Description = entity.Description,
                Products = new List<ProductModel>()
            };
            foreach (var item in entity.Products)
            {
                model.Products.Add(ProductMapper.ToModel(item, 0));
            }

            return model;

        }

        public static Category ToEntity(Category entity, CategoryModel model)
        {
            if (model == null) return null;
            entity.CategoryID = model.CategoryID;
            entity.CategoryName = model.CategoryName;
            entity.Description = model.Description;

            return entity;
        }
        #endregion

        #region Methods Without Relation
        public static CategoryModel ToModel(Category entity, int childLevel)
        {
            if (entity == null) return null;
            CategoryModel model = new CategoryModel()
            {
                CategoryID = entity.CategoryID,
                CategoryName = entity.CategoryName,
                Description = entity.Description
            };

            if (childLevel > 0)
            {
                model.Products = new List<ProductModel>();
                foreach (var item in entity.Products)
                {
                    model.Products.Add(ProductMapper.ToModel(item, childLevel - 1));
                }
            }

            return model;
        }

        #endregion
    }
}
